﻿using Hidistro.Core;
using Hidistro.Entities.Orders;
using Hidistro.SaleSystem.Member;
using Hidistro.SaleSystem.Shopping;
using Hidistro.UI.Common.Controls;
using Hidistro.UI.SaleSystem.Tags;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.CodeBehind
{
    public class FinishOrderNo : HtmlTemplatedWebControl
    {
        private Button btnSubMitOrder;
        private Literal litOrderId;
        private FormatedMoneyLabel litOrderPrice;
        private Literal litPaymentName;
        private HyperLink hlinkOrderDetails;
        private string orderId;

        private Literal toName;
        private Literal Adder;
        private Literal ordertime;
        private Literal productimg;
        private Literal orderStatus;
        private Literal ItemAdjustedPrice;
        private Literal Dh;
        private YB_OrderItems orderItems;
        


        protected override void AttachChildControls()
        {
            if (this.Page.Request.QueryString["orderId"] == null)
            {
                base.GotoResourceNotFound();
            }
            this.orderId = this.Page.Request.QueryString["orderId"];

            JSShop.Model.Hishop_Orders oredermodel = new Juse.Interface.UserOrder().GetModelByOrderId(this.orderId);
            List<JSShop.Model.Hishop_OrderItems> list = new Juse.Interface.UserOrder().GetItemsByOrderId(this.orderId);
             
           // OrderInfo orderInfo = ShoppingProcessor.GetOrderInfo(this.orderId);
            if (oredermodel != null)
            {
                
                if (oredermodel.PaymentTypeId != 1)
                {
                    if (oredermodel.Gateway != "hishop.plugins.payment.advancerequest")
                    {
                        HttpContext.Current.Response.Redirect(Globals.GetSiteUrls().UrlData.FormatUrl("sendPayment", new object[] { this.orderId }));
                    }
                    else
                    {
                        HttpContext.Current.Response.Redirect(string.Format("/user/pay.aspx?OrderId={0}", this.orderId));
                    }
                }
            }

            this.litOrderPrice = (FormatedMoneyLabel)this.FindControl("litOrderPrice");

            this.toName = (Literal)this.FindControl("toName");
            this.Adder = (Literal)this.FindControl("Adder");
            this.ordertime = (Literal)this.FindControl("ordertime");
            this.ItemAdjustedPrice = (Literal)this.FindControl("ItemAdjustedPrice");
            this.orderItems = (YB_OrderItems)this.FindControl("YB_OrderItems");
            this.orderStatus = (Literal)this.FindControl("orderStatus");

            this.litPaymentName = (Literal)this.FindControl("litPaymentName");
            this.Dh = (Literal)this.FindControl("Dh");
            this.btnSubMitOrder = (Button)this.FindControl("btnSubMitOrder");
            this.hlinkOrderDetails = (HyperLink)this.FindControl("hlinkOrderDetails");
           // this.btnSubMitOrder.Click += new EventHandler(this.btnSubMitOrder_Click);
            this.hlinkOrderDetails.NavigateUrl = "#"; //Globals.GetSiteUrls().UrlData.FormatUrl("user_OrderDetails", this.orderId);

            this.hlinkOrderDetails.Text = this.orderId;
            this.orderItems.DataSource = list;
            this.orderItems.DataBind();
            if (!this.Page.IsPostBack)
            {
                this.LoadOrderInfo(oredermodel);
            }
        }

        private void btnSubMitOrder_Click(object sender, EventArgs e)
        {
            OrderInfo orderInfo = ShoppingProcessor.GetOrderInfo(this.orderId);
            if (orderInfo != null)
            {
                if (orderInfo.Gateway != "hishop.plugins.payment.advancerequest")
                {
                    HttpContext.Current.Response.Redirect(Globals.GetSiteUrls().UrlData.FormatUrl("sendPayment", new object[] { this.orderId }));
                }
                else
                {
                    HttpContext.Current.Response.Redirect(string.Format("/user/pay.aspx?OrderId={0}", this.orderId));
                }
            }
        }

        public void LoadOrderInfo(JSShop.Model.Hishop_Orders orderInfo)
        {

            if (orderInfo != null)
            {
                this.ordertime.Text = orderInfo.OrderDate.ToString();
                this.toName.Text = orderInfo.ShipTo;
                try
                {
                    this.Adder.Text = orderInfo.ShippingRegion.ToString().Split('，')[0];

                }
                catch { this.Adder.Text = orderInfo.ShippingRegion; }
                this.orderStatus.Text = GetOrderStatusName((Hidistro.Entities.Orders.OrderStatus)orderInfo.OrderStatus);
                try
                {
                    this.Dh.Text = orderInfo.CellPhone.Substring(0, 3) + "****" + orderInfo.CellPhone.Substring(orderInfo.CellPhone.Length - 4, 4);
                    
                }
                catch { this.Dh.Text = orderInfo.CellPhone; }

                this.litOrderPrice.Money = orderInfo.OrderTotal;
                string arc = orderInfo.PaymentType;

                this.litPaymentName.Text = arc;


                if (orderInfo.Gateway == "hishop.plugins.payment.podrequest")
                {
                    //this.btnSubMitOrder.Visible = false;
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-FinishOrderNo.html";
            }
            base.OnInit(e);
        }

        public static string GetOrderStatusName(Hidistro.Entities.Orders.OrderStatus orderStatus)
        {
            switch (orderStatus)
            {
                case Hidistro.Entities.Orders.OrderStatus.WaitBuyerPay:
                    return "等待买家付款";

                case Hidistro.Entities.Orders.OrderStatus.BuyerAlreadyPaid:
                    return "已付款,等待发货";

                case Hidistro.Entities.Orders.OrderStatus.SellerAlreadySent:
                    return "已发货";

                case Hidistro.Entities.Orders.OrderStatus.Closed:
                    return "已关闭";

                case Hidistro.Entities.Orders.OrderStatus.Finished:
                    return "订单已完成";

                case Hidistro.Entities.Orders.OrderStatus.ApplyForRefund:
                    return "申请退款";

                case Hidistro.Entities.Orders.OrderStatus.ApplyForReturns:
                    return "申请退货";

                case Hidistro.Entities.Orders.OrderStatus.ApplyForReplacement:
                    return "申请换货";

                case Hidistro.Entities.Orders.OrderStatus.Refunded:
                    return "已退款";

                case Hidistro.Entities.Orders.OrderStatus.Returned:
                    return "已退货";

                case Hidistro.Entities.Orders.OrderStatus.History:
                    return "历史订单";
            }
            return "-";
        }
    }
}
